/*

Copyright 2011 OPTi365.com

License: CC BY-SA 3.0



The following codes illustrate a simple way to generate "eco credit for images" for a list of 5 images.
To see the live demo, go to
       http://opti365.googlecode.com/hg/eco-image/o/OPTi365.com/sample-frame.html
       
Note: The wood frames have additional special tags. 
      For example, you can also add the "year" tags to override the default year for some images.

============ Sample HTML JavaScript Codes (BEGIN) ============

<script type="text/javascript" src="http://opti365.googlecode.com/hg/eco-code/OPTi365-eco-credit-image.js"></script>


Explore more: 
<span class="opti365_eco_navigation_to_be_replaced"><a href=".">Current Folder</a></span>
<br><br>
<script type="text/javascript">
opti365_ecocreditimage.replaceNavigation();  //generate the navigation links
</script>

<script type="text/javascript">

//NOTE: add additional credit tags right after the URL
list = [                                                                                                                         
'https://lh3.googleusercontent.com/__Gzz4zXugb8/TdqmaAusrFI/AAAAAAAAAF0/1PFJOwxJAc8/OPTi365_frame-wood-520x306.gif',             { "view": "520,306,16,16,488,274" },
'https://lh4.googleusercontent.com/__Gzz4zXugb8/TdqmaJXBnoI/AAAAAAAAAFw/MNovX3vEb6c/OPTi365_frame-wood-520x390.gif',             { "view": "520,390,16,12,488,366" },
'https://lh6.googleusercontent.com/__Gzz4zXugb8/TdqmZuCtk1I/AAAAAAAAAFo/AinVUi4U0mo/OPTi365_frame-black-gradient-520x390.png',
'https://lh5.googleusercontent.com/__Gzz4zXugb8/TdqmZ-8LtzI/AAAAAAAAAFs/wH0yMlLJfMg/OPTi365_frame-black50-520x292.png',
'https://lh3.googleusercontent.com/__Gzz4zXugb8/TdqmZn6aEAI/AAAAAAAAAFk/vETMEI3A4_8/OPTi365_frame-black50-520x292-no-logo.png',
''
];

credit = {
  "license":"CC BY-SA 3.0",
  "author":"OPTi365.com",
  "author_url":"http://www.opti365.com",
  "year":"2011", 
  "sample_url":document.URL //CAUTION: must be a live website to get the correct URL
};

opti365_ecocreditimage.showList('', list, credit, true);  //generate all the samples

</script>
============ Sample HTML JavaScript Codes (END) ============


*/


opti365_ecocreditimage = {

generateCreditURI: function(credit) {
  var s = '';
  for (var tag in credit)
    s = this.appendURIBack(s, tag, credit[tag]);
  return s;
},

generateCreditURI2: function(credit, special_credit) {
  var s = this.generateCreditURI(special_credit);
  for (var tag in credit)
    if (!(tag in special_credit))
      s = this.appendURIBack(s, tag, credit[tag]);
  return s;
},

getBaseURL: function() {
  return document.URL.substr(0,document.URL.lastIndexOf('/')+1);
},

makeShorterTagValue: function(tag, value) {
  var m;
  if (/url$/.test(tag)) {
    if (m=value.match(/^http:\/\/(goo\.gl\/)?(.*)/))
      value = '*'+(m[1]?'g':'')+'*'+m[2];
  }      
  return tag+'='+encodeURIComponent(value);
},

appendURIFront: function(s, tag, value) {
  return this.makeShorterTagValue(tag,value)+(s?'&'+s:'');
},

appendURIBack: function(s, tag, value) {
  return (s?s+'&':'')+this.makeShorterTagValue(tag,value);
},

appendURIBackFromCredits: function(s, tag, credit, scredit) {
  if (scredit[tag])
    return (s?s+'&':'')+this.makeShorterTagValue(tag, scredit[tag]);
  else
    if (credit[tag])
      return (s?s+'&':'')+this.makeShorterTagValue(tag, credit[tag]);
  return s;
},
      

joinURIs: function(s1, s2) {
  return s1?(s2?s1+'&'+s2:s1):s2;
},

showList: function (baseURL, list, credit, nameRemoveExtension, nameRemovePrefixString) {
  var i, len=list.length, scredit;
  
  if (baseURL=='.')
    baseURL = this.getBaseURL();
  
  for (i=0; i<len; i++) {
    var entry = list[i], name, p, url, scredit, creditURI='';
    
    if (entry) {
      if (i+1<len && typeof(list[i+1])!='string') {
        i++;
        scredit = list[i];
      } else {
        scredit = {};
      }
      
      if (!(name=scredit['name'])) {
        name = (p=entry.lastIndexOf('/'))<0?entry:entry.substr(p+1);
        if (nameRemovePrefixString && nameRemovePrefixString==name.substr(0, nameRemovePrefixString.length))
          name = name.substr(nameRemovePrefixString.length);
        if (nameRemoveExtension) {
          p = name.lastIndexOf('.');
          if (p>0)
            name = name.substr(0,p);
        }
      }
      
      creditURI = this.appendURIBackFromCredits(creditURI, 'view', credit, scredit);
      creditURI = this.appendURIBackFromCredits(creditURI, 'url', credit, scredit);
      creditURI = creditURI.replace(/%2c/ig, ',').replace(/%2f/ig, '/');
      
      url = baseURL+entry+(creditURI?'#'+creditURI:'');
      
      document.write('<h3>'+name+'</h3>',
        'Image URL with Eco Credit:', scredit['view']?'(*with view tag)':'', '<br>',
        'URL as Link: [<a href="'+url+'">URL</a>]<br>',
        'URL as Text: <input style="width:300px" value="'+url+'" /><br>',
        'URL for Viewer: [<a href="http://www.opti365.com/eco-credit-image?url='+encodeURIComponent(url)+'">Viewer</a>] to show details in Eco Credit for Image<br>',
        scredit['x-source']?'Source: <a href="'+scredit['x-source']+'">'+scredit['x-source']+'</a><br>':'',
        '<a href="'+url+'"><img border=0 src="'+entry+'" /" title="click to see full image"></a><br><br>');
    }
  }
},

replaceNavigation: function() {
  var list = document.getElementsByTagName('span'), i, len=list.length;
  for (i=0; i<len; i++) {
    if (/opti365_eco_navigation_to_be_replaced/.test(list[i].className))
      list[i].innerHTML = '[<a href="http://opti365.googlecode.com/hg/eco-image/0-sample-pointers.html">Top Level</a>] [<a href=".">Current Folder</a>]';
  }
  
},

showAdForGoogleAds: function() {
  document.write('<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"><\/script>');
},

showAdForOpti365_banner: function() {
  google_ad_client = "ca-pub-4655531702108418";
  google_ad_slot = "7168661640"; /* zosc728x90 */
  google_ad_width = 728;
  google_ad_height = 90;  
  this.showAdForGoogleAds();
},

showAdForOpti365_rectangle: function() {
  google_ad_client = "ca-pub-4655531702108418";
  google_ad_slot = "8829062655"; /* zosc336x280 */
  google_ad_width = 336;
  google_ad_height = 280;  
  this.showAdForGoogleAds();
},

showAd: function(myAdFunction, donatePercentNumber, wantDebugMessage) {
  var effectiveDonatePercentNumber = 100, isDonated=true;
  if (typeof myAdFunction=='function') {
    if (donatePercentNumber==undefined || donatePercentNumber<0 || donatePercentNumber>100)
      effectiveDonatePercentNumber = 50; //wrong number, so set to default 50 percent sharing
    else
      effectiveDonatePercentNumber = donatePercentNumber;
      
    isDonated = Math.random() < effectiveDonatePercentNumber/100;
  }
  
  document.write('<div style="font-size:smaller">Ad Sharing (with <span style="font-weight:bold">'+effectiveDonatePercentNumber+'%</span> donation)<br>Now showing <span style="font-weight:bold">', isDonated?'site owner':'page owner', '</span>\'s ad</div>');
  if (isDonated)
    this.showAdForOpti365_rectangle();
  else
    myAdFunction();
},

showShareGadget: function() {
  opti365.shareJS_setup({gcs:"sl=*0~tr=y~wlimit=120~sty=float:right;~ag=n"});
},

loadCSS: function() {
  document.write('<style>.opti365_ecocreditimage_background{background:url(http://opti365.googlecode.com/hg/eco-image/o/OPTi365.com/pattern/OPTi365_pattern-brick-white-gray.gif); margin:0px -10px; padding:15px;}</style>');
}

}//end of opti365_ecocreditimage
